package com.huadao.cart_ccs.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseHelper;
import com.huadao.cart_ccs.common.UtilHelpers;
import com.huadao.cart_ccs.common.dialog.effects.BaseEffects;
import com.huadao.cart_ccs.common.dialog.effects.RockButtonAttr;

import java.util.List;

public class RockDialog extends Dialog implements DialogInterface {

    private LinearLayout mLinearLayoutView;

    private RelativeLayout mRelativeLayoutView;

    private LinearLayout mLinearLayoutMsgView;

    private LinearLayout mLinearLayoutTopView;

    private FrameLayout mFrameLayoutCustomView;

    private View mDialogView;

    private View mDivider;

    private TextView mTitle;

    private ImageView close;

    private TextView mMessage;

    private ImageView mIcon;

    private LinearLayout ll_button;

    public RockDialog(Context context) {
        super(context);
        init(context);

    }

    public RockDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.width = UtilHelpers.dp2px(getContext(), 530);
//        params.width = (int) (BaseHelper.getScreenWidth(getContext()) * 0.7);
        params.gravity = Gravity.CENTER;
        getWindow().setAttributes(params);
    }

    public static RockDialog getInstance(Context context, int style) {
        return new RockDialog(context, style);

    }

    private void init(Context context) {
        mDialogView = View.inflate(context, R.layout.dialog_layout, null);

        mLinearLayoutView = mDialogView.findViewById(R.id.parentPanel);
        mRelativeLayoutView = mDialogView.findViewById(R.id.main);
        mLinearLayoutTopView = mDialogView.findViewById(R.id.topPanel);
        mLinearLayoutMsgView = mDialogView.findViewById(R.id.contentPanel);
        mFrameLayoutCustomView = mDialogView.findViewById(R.id.customPanel);

        mTitle = mDialogView.findViewById(R.id.alertTitle);
        close = mDialogView.findViewById(R.id.close);
        mMessage = mDialogView.findViewById(R.id.message);
        mIcon = mDialogView.findViewById(R.id.icon);
        mDivider = mDialogView.findViewById(R.id.titleDivider);
        ll_button = mDialogView.findViewById(R.id.ll_button);

        setContentView(mDialogView);

        //避免在点击dialog时dialog消失。
        mLinearLayoutView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void applyAttribute(final RockAttribute attribute, Context context) {
        mDivider.setBackgroundColor(attribute.getDevideColor());
        if (!TextUtils.isEmpty(attribute.getTitle())) {
            mTitle.setText(attribute.getTitle());
        }
        mTitle.setTextColor(attribute.getTitleColor());
        if (!TextUtils.isEmpty(attribute.getMessage())) {
            mMessage.setText(attribute.getMessage());
        } else {
            mMessage.setVisibility(View.GONE);
        }
        mMessage.setTextColor(attribute.getMessageColor());
        mLinearLayoutView.getBackground().setColorFilter(ColorUtils.getColorFilter(attribute.getDialogColor()));
        if (attribute.getIconResId() != 0) {
            mIcon.setImageResource(attribute.getIconResId());
        } else {
            mIcon.setVisibility(View.GONE);
        }
        if (attribute.isButtonList()) {
            ll_button.setOrientation(LinearLayout.VERTICAL);
        } else {
            ll_button.setOrientation(LinearLayout.HORIZONTAL);
        }

        if (attribute.isButtonList()) {
            ll_button.setOrientation(LinearLayout.VERTICAL);
        } else {
            ll_button.setOrientation(LinearLayout.HORIZONTAL);
        }
        List<RockButtonAttr> rockButtonAttrList = attribute.getButtons();
        for (RockButtonAttr rockButtonAttr : rockButtonAttrList) {
            addButton(rockButtonAttr, context);
        }

        RockDialog.this.setCanceledOnTouchOutside(attribute.isCancelable());
        mRelativeLayoutView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (attribute.isCancelable()) dismiss();
            }
        });

        if (attribute.getCustomView() != null) {
            if (mFrameLayoutCustomView.getChildCount() > 0) {
                mFrameLayoutCustomView.removeAllViews();
            }
            mFrameLayoutCustomView.addView(attribute.getCustomView());
            mFrameLayoutCustomView.setVisibility(View.VISIBLE);
            mLinearLayoutMsgView.setVisibility(View.GONE);
        }

        if (attribute.getmDuration() != -1) {
            this.setOnShowListener(new OnShowListener() {
                @Override
                public void onShow(DialogInterface dialogInterface) {
                    mLinearLayoutView.setVisibility(View.VISIBLE);
                    if (attribute.getType() == null) {
                        attribute.setType(Effectstype.Slidetop);
                    }
                    start(attribute.getType(), attribute.getmDuration());
                }
            });
        }

    }

    private void addButton(final RockButtonAttr rockButtonAttr, Context context) {
        Button button = new Button(context);
        button.setText(rockButtonAttr.getText());
        button.setTextColor(ContextCompat.getColor(context, R.color.text_white));
        button.setTextSize(TypedValue.COMPLEX_UNIT_PX, rockButtonAttr.getTextSize());
        button.setBackgroundResource(rockButtonAttr.getBgResId());
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rockButtonAttr.getRockCallBack() != null) {
                    rockButtonAttr.getRockCallBack().onClick(RockDialog.this, v);
                }
            }
        });
        button.setMinHeight(UtilHelpers.dp2px(context, 36));
        button.setGravity(Gravity.CENTER);
        ll_button.addView(button);

//        LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) ll_button.getLayoutParams();
        LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(button.getLayoutParams());
        if(ll_button.getOrientation()==LinearLayout.HORIZONTAL) {
            buttonParams.weight = 1;
            buttonParams.width = 0;
            buttonParams.height = UtilHelpers.dp2px(context, 50);
        }else if(ll_button.getOrientation()==LinearLayout.VERTICAL){
            buttonParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
            buttonParams.height = UtilHelpers.dp2px(context, 50);
        }
        buttonParams.bottomMargin = UtilHelpers.dp2px(context, 10);
        int margin = UtilHelpers.dp2px(context, 8);
        buttonParams.setMargins(margin, margin, margin, margin);
        button.setLayoutParams(buttonParams);
    }

    @Override
    public void show() {
        super.show();
    }

    private void start(Effectstype type, int mDuration) {
        BaseEffects animator = type.getAnimator();
        if (mDuration != -1) {
            animator.setDuration(Math.abs(mDuration));
        }
        animator.start(mRelativeLayoutView);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    public static class RockDialogBuilder {
        private final RockAttribute attribute;
        private Context mContext;
        private int style = R.style.dialog_untran;
        private int textSize;

        public RockDialogBuilder(Context mContext, int style) {
            this.mContext = mContext;
            this.style = style;
            attribute = new RockAttribute();
            attribute.toDefault(mContext);
//            textSize = mContext.getResources().getDimensionPixelSize(R.dimen.textSize_normal);
            textSize = mContext.getResources().getDimensionPixelSize(R.dimen.dialogTextBtn);
        }

        public RockDialogBuilder(Context mContext) {
            this.mContext = mContext;
            attribute = new RockAttribute();
            attribute.toDefault(mContext);
//            textSize = mContext.getResources().getDimensionPixelSize(R.dimen.textSize_normal);
            textSize = mContext.getResources().getDimensionPixelSize(R.dimen.dialogTextBtn);
        }

        public RockDialog create() {
            RockDialog rockDialog = RockDialog.getInstance(mContext, style);
            rockDialog.applyAttribute(attribute, mContext);
            return rockDialog;
        }

        public RockDialogBuilder withDividerColor(String colorString) {
            attribute.setDevideColor(Color.parseColor(colorString));
            return this;
        }

        public RockDialogBuilder withDividerColor(int color) {
            attribute.setDevideColor(color);
            return this;
        }


        public RockDialogBuilder withTitle(CharSequence title) {
            attribute.setTitle(title);
            return this;
        }

        public RockDialogBuilder withTitleColor(String colorString) {
            attribute.setTitleColor(Color.parseColor(colorString));
            return this;
        }

        public RockDialogBuilder withTitleColor(int color) {
            attribute.setTitleColor(color);
            return this;
        }

        public RockDialogBuilder withMessage(int textResId) {
            attribute.setMessage(mContext.getString(textResId));
            return this;
        }

        public RockDialogBuilder withMessage(CharSequence msg) {
            attribute.setMessage(msg);
            return this;
        }

        public RockDialogBuilder withMessageColor(String colorString) {
            attribute.setMessageColor(Color.parseColor(colorString));
            return this;
        }

        public RockDialogBuilder withMessageColor(int color) {
            attribute.setMessageColor(color);
            return this;
        }

        public RockDialogBuilder withDialogColor(String colorString) {
            attribute.setDialogColor(Color.parseColor(colorString));
            return this;
        }

        public RockDialogBuilder withDialogColor(int color) {
            attribute.setDialogColor(color);
            return this;
        }

        public RockDialogBuilder withIcon(int drawableResId) {
            attribute.setIconResId(drawableResId);
            return this;
        }

        public RockDialogBuilder withDuration(int duration) {
            attribute.setmDuration(duration);
            return this;
        }

        public RockDialogBuilder withEffect(Effectstype type) {
            attribute.setType(type);
            return this;
        }

        public RockDialogBuilder addButton(String button, RockCallBack rockCallBack) {
            RockButtonAttr rockButtonAttr = new RockButtonAttr(button, rockCallBack);
            rockButtonAttr.setTextSize(textSize);
            attribute.addButton(rockButtonAttr);
            return this;
        }

        public RockDialogBuilder addButton(String button, int nature, RockCallBack rockCallBack) {
            RockButtonAttr rockButtonAttr = new RockButtonAttr(button, nature, rockCallBack);
            rockButtonAttr.setTextSize(textSize);
            attribute.addButton(rockButtonAttr);
            return this;
        }

        public RockDialogBuilder withButtonList(boolean isList) {
            attribute.setButtonList(isList);
            return this;
        }

        public RockDialogBuilder withCustomView(int resId) {
            attribute.setCustomView(View.inflate(mContext, resId, null));
            return this;
        }

        public RockDialogBuilder withCustomView(View view) {
            attribute.setCustomView(view);
            return this;
        }

        public RockDialogBuilder isCancelableOnTouchOutside(boolean cancelable) {
            attribute.setCancelable(cancelable);
            return this;
        }

    }
}